package com.jy.rhin.infra.repo.notfication.publish.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.jy.rhin.infra.repo.notfication.publish.po.PublishMessagePO;
import com.jy.rhin.sdk.dto.notification.SubscriberShortDto;
import com.jy.rhin.sdk.dto.notification.resp.PublishMessageDetailDto;
import com.jy.rhin.sdk.dto.notification.resp.PublishMessageDto;
import com.jy.rhin.sdk.dto.notification.resp.PublishMessagePageDto;
import com.jy.rhin.sdk.dto.notification.resp.SubscribeAndFilterDto;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * @author WeiQ
 * @date 2022/6/29 17:08
 */
@Repository
public interface PublishMessageMapper extends BaseMapper<PublishMessagePO> {
    /**
     * 根据topicId查询订阅了该主题的所有订阅者以及对应的过滤条件
     *
     * @param topicId
     * @return
     */
    @Select("select d.publisher_code                    as publisherCode,                                              \n" +
            "       f.participant_name                    as publisherName,                                            \n" +
            "       a.subscriber_code                   as subscriberCode,                                             \n" +
            "       e.participant_name                   as subscriberName,                                            \n" +
            "       a.subscribe_uid                     as subscribeUid,                                               \n" +
            "       a.topic_exp                         as topicExp,                                                   \n" +
            "       a.id                                as subscribeId,                                                \n" +
            "       d.id                                as topicId,                                                    \n" +
            "       b.query_filter_exp                  as queryFilterExp,                                             \n" +
            "       b.query_filter_value                as queryFilterValue,                                           \n" +
            "       c.target_path                       as filterJsonPath,                                             \n" +
            "       c.compare_exp                       as compareExp,                                                 \n" +
            "       e.participant_url                   as addressUrl,                                                 \n" +
            "       e.interaction_type                  as interactionType                                             \n" +
            "from t_notification_subscribe a                                                                           \n" +
            "         LEFT JOIN t_notification_subscribe_filter b on a.id = b.subscribe_id                             \n" +
            "         LEFT JOIN t_notification_topic_query_filter c on a.topic_exp = c.topic_exp                       \n" +
            "    and a.query_exp = c.query_exp and b.query_filter_exp = c.filter_exp                                   \n" +
            "         LEFT JOIN t_notification_topic d on a.topic_exp = d.topic_exp                                    \n" +
            "         LEFT JOIN t_notification_participant e on a.subscriber_code = e.participant_code                 \n" +
            "    and e.participant_role = 'SUBSCRIBER'                                                                 \n" +
            "         LEFT JOIN t_notification_participant f ON d.publisher_code = f.participant_code                  \n" +
            "    AND f.participant_role = 'PUBLISHER'                                                                  \n" +
            "where  a.status= 'SUBSCRIBED' and d.id = #{topicId}                                                         ")
    List<SubscribeAndFilterDto> querySubscribeAndFilterListByTopicId(@Param("topicId") Long topicId);

    /**
     * 分页查询
     *
     * @param page
     * @param queryWrapper
     * @return
     */
    @Select("SELECT a.id,                                                                                                                  \n" +
            "       c.subscribe_uid as subscriptionId,                                                                                     \n" +
            "       d.topic_name as topicName,                                                                                             \n" +
            "       b1.participant_name as publisherName,                                                                                  \n" +
            "       b2.participant_name as subscriberName,                                                                                 \n" +
            "       if(a.notify_status = 'DONE',TRUE, FALSE) as notifyStatus,                                                              \n" +
            "       a.notify_time as notifyTime                                                                                            \n" +
            "from  t_notification_log a                                                                                                    \n" +
            "   LEFT JOIN t_notification_participant b1 ON a.publisher_code = b1.participant_code AND b1.participant_role = 'PUBLISHER'    \n" +
            "   LEFT JOIN t_notification_participant b2 ON a.subscriber_code = b2.participant_code AND b2.participant_role = 'SUBSCRIBER'  \n" +
            "   LEFT JOIN t_notification_subscribe c ON a.subscribe_id = c.id                                                              \n" +
            "   LEFT JOIN t_notification_topic d on a.topic_id = d.id                                                                      \n" +
            "${ew.customSqlSegment}                                                                                                         ")
    com.baomidou.mybatisplus.extension.plugins.pagination.Page<PublishMessagePageDto> queryPublishMessagePage(com.baomidou.mybatisplus.extension.plugins.pagination.Page<PublishMessagePageDto> page, @Param(Constants.WRAPPER) QueryWrapper<PublishMessagePageDto> queryWrapper);

    /**
     * 详情查询
     *
     * @param queryWrapper
     * @return
     */
    @Select("SELECT a.id,                                                                                                                  \n" +
            "       c.subscribe_uid as subscriptionId,                                                                                     \n" +
            "       d.topic_name as topicName,                                                                                             \n" +
            "       b1.participant_name as publisherName,                                                                                  \n" +
            "       b2.participant_name as subscriberName,                                                                                 \n" +
            "       if(a.notify_status = 'DONE',TRUE, FALSE) as notifyStatus,                                                              \n" +
            "       a.notify_time as notifyTime,                                                                                           \n" +
            "       d.topic_exp as topicExp,                                                                                               \n" +
            "       e.content                                                                                                              \n" +
            "from  t_notification_log a                                                                                                    \n" +
            "   LEFT JOIN t_notification_participant b1 ON a.publisher_code = b1.participant_code AND b1.participant_role = 'PUBLISHER'    \n" +
            "   LEFT JOIN t_notification_participant b2 ON a.subscriber_code = b2.participant_code AND b2.participant_role = 'SUBSCRIBER'  \n" +
            "   LEFT JOIN t_notification_subscribe c ON a.subscribe_id = c.id                                                              \n" +
            "   LEFT JOIN t_notification_topic d on a.topic_id = d.id                                                                      \n" +
            "   LEFT JOIN t_notification_message e on a.message_id = e.id                                                                  \n" +
            "${ew.customSqlSegment}                                                                                                         ")
    PublishMessageDetailDto queryPublishMessageDetail(@Param(Constants.WRAPPER) QueryWrapper<PublishMessageDetailDto> queryWrapper);

    /**
     * 获取发送失败的消息列表
     *
     * @param publishCount
     * @param startTime
     * @param endTime
     * @param page
     * @return
     */
    @Select("SELECT a.id,                                                                                               \n" +
            "       a.message_id      as messageId,                                                                     \n" +
            "       a.publisher_code  as publisherCode,                                                                 \n" +
            "       a.subscriber_code as subscriberCode,                                                                \n" +
            "       a.topic_id        as topicId,                                                                       \n" +
            "       a.subscribe_id    as subscribeId,                                                                   \n" +
            "       f.topic_exp       as topicExp,                                                                      \n" +
            "       a.notify_status   as notifyStatus,                                                                  \n" +
            "       a.publish_count   as publishCount,                                                                  \n" +
            "       a.notify_time     as notifyTime,                                                                    \n" +
            "       a.create_time     as createTime,                                                                    \n" +
            "       c.subscribe_uid   as subscribeUid,                                                                  \n" +
            "       d.participant_url AS addressUrl,                                                                    \n" +
            "       d.interaction_type AS interactionType,                                                              \n" +
            "       e.content         as message                                                                        \n" +
            "from t_notification_log a                                                                                  \n" +
            "         LEFT JOIN t_notification_subscribe c ON a.subscribe_id = c.id                                     \n" +
            "         LEFT JOIN t_notification_topic f ON a.topic_id = f.id                                             \n" +
            "         LEFT JOIN t_notification_participant d                                                            \n" +
            "                   ON a.subscriber_code = d.participant_code AND d.participant_role = 'SUBSCRIBER'         \n" +
            "         LEFT JOIN t_notification_message e ON a.message_id = e.id                                         \n" +
            "WHERE a.notify_status <> 'DONE'                                                                            \n" +
            "  and a.publish_count < #{publishCount}                                                                    \n" +
            "  and a.create_time between #{startTime} AND #{endTime}                                                   ")
    com.baomidou.mybatisplus.extension.plugins.pagination.Page<PublishMessageDto> querySendFailPublishMessageList(IPage<PublishMessageDto> page, @Param("publishCount") int publishCount, @Param("startTime") Date startTime, @Param("endTime") Date endTime);


    /**
     * 统计指定日期通知次数
     *
     * @param queryWrapper
     * @return
     */
    @Select(" select count(1) as total from t_notification_log ${ew.customSqlSegment} ")
    Integer countDateNotification(@Param(Constants.WRAPPER) QueryWrapper<PublishMessagePO> queryWrapper);

    /**
     * 根据消息id获取消息订阅者信息列表
     * @param queryWrapper
     * @return
     */
    @Select(" select b.id as id, b.participant_code as subscriberCode, b.participant_name as subscriberName \n" +
            " from t_notification_log a                                                                     \n" +
            " left join t_notification_participant b                                                        \n" +
            " on a.subscriber_code = b.participant_code and b.participant_role = 'SUBSCRIBER'               \n" +
            " ${ew.customSqlSegment}                                                                        ")
    List<SubscriberShortDto> queryMessageSubscriberListByMsgId(@Param(Constants.WRAPPER) QueryWrapper<SubscriberShortDto> queryWrapper);
}
